//
//  HomeTransforOrderInfoSecondTwoTableViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/4.
//

#import "HomeTransforOrderInfoSecondTwoTableViewCell.h"
#import "HomeTranforOrderGoodsInfoTableViewCell.h"


@interface HomeTransforOrderInfoSecondTwoTableViewCell ()<UITableViewDelegate,UITableViewDataSource,protocolChooseToolCellDegate>


@property (nonatomic,strong) UILabel * InfoLabel;
@property (nonatomic,strong) UILabel * nameTitleLabel;
@property (nonatomic,strong) UILabel * sizeTitleLabel;
@property (nonatomic,strong) UILabel * numTitleLabel;
@property (nonatomic,strong) UIButton * selectBtn;

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) NSMutableArray * CellToolsTemp;
@property (nonatomic,assign)     NSInteger IsType;


@end



@implementation HomeTransforOrderInfoSecondTwoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self){
     
        
        [self configUI];
        
        // 添加通知监听
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                               selector:@selector(clearSelection:) 
                                                   name:@"ClearOtherAreaSelection" 
                                                 object:nil];
        
    }
    
    return self;
}

-(void)setType:(NSInteger)type{
    _type = type;
}

-(void)setModel:(HomeTransforOrderInfoModel *)model{
    _model = model;
    
    if (self.type == 1){
        self.InfoLabel.text = @"带入控制区明细";
        self.dataSource = [NSMutableArray arrayWithArray:[HomeTransforOrderInfoAreaListModel mj_objectArrayWithKeyValuesArray:self.model.inList]];
    }else if (self.type == 2){
        self.InfoLabel.text = @"带出控制区明细";
        self.dataSource = [NSMutableArray arrayWithArray:[HomeTransforOrderInfoAreaListModel mj_objectArrayWithKeyValuesArray:self.model.outList]];
    }
    
    NSInteger hei = self.dataSource.count * ALIGN_HEIGHT_PT(32);
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(hei));
    }];
    
    [self.tableView reloadData];
    
}







-(void)configUI{
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.InfoLabel];
    [self.contentView addSubview:self.nameTitleLabel];
    [self.contentView addSubview:self.sizeTitleLabel];
    [self.contentView addSubview:self.numTitleLabel];
    [self.numTitleLabel addSubview:self.selectBtn];
    [self.contentView addSubview:self.tableView];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.contentView).with.offset(ALIGN_HEIGHT_PT(5));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.InfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(5));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.InfoLabel.mas_left);
        make.top.equalTo(self.InfoLabel.mas_bottom);
        make.size.equalTo(@(CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(24)) / 3, ALIGN_HEIGHT_PT(36))));
    }];

    
    [self.sizeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameTitleLabel.mas_right);
        make.centerY.equalTo(self.nameTitleLabel.mas_centerY);
        make.size.equalTo(@(CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(24)) / 3, ALIGN_HEIGHT_PT(36))));
    }];
    
    
    [self.numTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sizeTitleLabel.mas_right);
        make.centerY.equalTo(self.nameTitleLabel.mas_centerY);
        make.size.equalTo(@(CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(24)) / 3, ALIGN_HEIGHT_PT(36))));
    }];
    
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.numTitleLabel.mas_centerY);
        make.right.equalTo(self.numTitleLabel.mas_right).with.offset(-ALIGN_WIDTH_PT(14.5));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(16), ALIGN_WIDTH_PT(16))));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.contentView);
        make.top.equalTo(self.nameTitleLabel.mas_bottom);
//        make.height.equalTo(@(ALIGN_HEIGHT_PT(64)));
    }];
    
    
    
    
}


#pragma mark -- 点击事件
-(void)respondsTo_selectBtn:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    if (sender.selected){
        [self.selectBtn setImage:[UIImage imageNamed:@"选择"] forState:(UIControlStateSelected)];
        self.IsType = 0;
        // 发送通知，通知其他区域清空选择状态
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ClearOtherAreaSelection" 
                                                          object:nil 
                                                        userInfo:@{@"type":@(self.type)}];
        
        [self.dataSource enumerateObjectsUsingBlock:^(HomeTransforOrderInfoAreaListModel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.IsSelect) {
                obj.IsSelect = NO;
            } else {
                obj.IsSelect = YES;
            }
        }];
                
        [self.CellToolsTemp removeAllObjects];
        [self.dataSource enumerateObjectsUsingBlock:^(HomeTransforOrderInfoAreaListModel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.IsSelect) {
                [self.CellToolsTemp addObject:obj];
            }
        }];
        
    }else{
        [self.selectBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        self.IsType = 1;
        [self.dataSource enumerateObjectsUsingBlock:^(HomeTransforOrderInfoAreaListModel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.IsSelect = NO;
        }];
        
        [self.CellToolsTemp removeAllObjects];
    }
    [self.tableView reloadData];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseToolDataSource:IsType:)]){
        [self.delegate chooseToolDataSource:self.CellToolsTemp IsType:self.IsType];
    }
    
}

#pragma mark -- protocolChooseToolCellDegate
-(void)chooseTransforOrderTool:(HomeTransforOrderInfoAreaListModel *)model{
    
    if (model.IsSelect){
        [self.CellToolsTemp addObject:model];
    }else{
        [self.CellToolsTemp removeObject:model];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseToolDataSource: IsType:)]){
        [self.delegate chooseToolDataSource:self.CellToolsTemp IsType:self.IsType];
    }
    
    
}





#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTranforOrderGoodsInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTranforOrderGoodsInfoTableViewCell"];
    if (!cell){
        cell  = [[HomeTranforOrderGoodsInfoTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTranforOrderGoodsInfoTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HomeTransforOrderInfoAreaListModel * model  = self.dataSource[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(32);
}


#pragma mark -- 懒加载

-(UILabel *)titleLabel{
    if (!_titleLabel){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}


-(UILabel *)InfoLabel{
    if (!_InfoLabel){
        _InfoLabel = [UILabel new];
        _InfoLabel.textColor = COLOR_RGB(50, 113, 237);
        _InfoLabel.font = FONT14;
        _InfoLabel.textAlignment = NSTextAlignmentLeft;
//        _InfoLabel.text = @"带入控制区明细";
    }
    return _InfoLabel;
}

-(UILabel *)nameTitleLabel{
    if (!_nameTitleLabel){
        _nameTitleLabel = [UILabel new];
        _nameTitleLabel.textColor = [UIColor blackColor];
        _nameTitleLabel.font = FONT14;
        _nameTitleLabel.textAlignment = NSTextAlignmentCenter;
        _nameTitleLabel.backgroundColor = COLOR_RGB(244, 244, 244);
        _nameTitleLabel.text = @"货物名称";
    }
    return _nameTitleLabel;
}



-(UILabel *)sizeTitleLabel{
    if (!_sizeTitleLabel){
        _sizeTitleLabel = [UILabel new];
        _sizeTitleLabel.textColor = [UIColor blackColor];
        _sizeTitleLabel.font = FONT14;
        _sizeTitleLabel.textAlignment = NSTextAlignmentCenter;
        _sizeTitleLabel.backgroundColor = COLOR_RGB(244, 244, 244);
        _sizeTitleLabel.text = @"规格尺寸";
    }
    return _sizeTitleLabel;
}



-(UILabel *)numTitleLabel{
    if (!_numTitleLabel){
        _numTitleLabel = [UILabel new];
        _numTitleLabel.textColor = [UIColor blackColor];
        _numTitleLabel.font = FONT14;
        _numTitleLabel.textAlignment = NSTextAlignmentCenter;
        _numTitleLabel.backgroundColor = COLOR_RGB(244, 244, 244);
        _numTitleLabel.text = @"数量";
        _numTitleLabel.userInteractionEnabled = YES;
    }
    return _numTitleLabel;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


-(UIButton *)selectBtn{
    if (!_selectBtn){
        _selectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.selectBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        [_selectBtn addTarget:self action:@selector(respondsTo_selectBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _selectBtn;
}
-(NSMutableArray *)CellToolsTemp{
    if (!_CellToolsTemp){
        _CellToolsTemp = [NSMutableArray array];
    }
    return _CellToolsTemp;
}

// 添加清空选择状态的方法
- (void)clearSelection:(NSNotification *)notification {
    NSNumber *type = notification.userInfo[@"type"];
    // 如果通知不是来自当前cell所在区域，则清空选择状态
    if (type.integerValue != self.type) {
        self.selectBtn.selected = NO;
        [self.selectBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        
        [self.dataSource enumerateObjectsUsingBlock:^(HomeTransforOrderInfoAreaListModel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.IsSelect = NO;
        }];
        
        [self.CellToolsTemp removeAllObjects];
        [self.tableView reloadData];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(chooseToolDataSource: IsType:)]){
            [self.delegate chooseToolDataSource:self.CellToolsTemp IsType:self.IsType];
        }
    }
}

// 在 dealloc 中移除通知监听
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
